import type { AppTreeData } from '../components/AppTree/types'

export interface List {
  value: string
  label: string
  children?: List[]
}
export interface Prop {
  value: string
  label: string
  children: string
}

const getKey = (prefix: string, id: number) => {
  return `${prefix}-${id}`
}

const createData = (
  maxDeep: number,
  maxChildren: number,
  minNodesNumber: number,
  deep = 1,
  key = 'node'
): List[] => {
  let id = 0
  return Array.from({ length: minNodesNumber })
    .fill(deep)
    .map(() => {
      const childrenNumber =
        deep === maxDeep ? 0 : Math.round(Math.random() * maxChildren)
      const nodeKey = getKey(key, ++id)
      return {
        value: nodeKey,
        label: nodeKey,
        children: childrenNumber
          ? createData(maxDeep, maxChildren, childrenNumber, deep + 1, nodeKey)
          : undefined
      }
    })
}

export default createData

export const ifHasChildren = (data: AppTreeData): boolean => {
  let result = false
  if (data.children && data.children.length) result = true
  return result
}
